package org.yibinxueyuan.drawcard.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    //shiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);

        //添加shiro的内置过滤器
        /**
         * anon:无需认证就可以访问
         * authc:必须认证了才能让访问
         * user: 必须拥有 记住我才能访问
         * perms: 拥有对某个资源的权限才能访问
         * role:拥有某个角色的权限才能访问
         */
        Map<String,String> filterMap = new LinkedHashMap<>();
        //设置必须登录认证了才能访问的页面

        //主页
        filterMap.put("/","authc");
        filterMap.put("/index","authc");

        filterMap.put("/cardmanage","authc");

        filterMap.put("/cardmanage","perms[user:root]");

        bean.setFilterChainDefinitionMap(filterMap);
        bean.setLoginUrl("/tologin");

        //设置没有权限跳转位置
        bean.setUnauthorizedUrl("/tologinpower");
        return bean;
/*
        //设置必须认证了才能访问
        filterMap.put("/test/hello","authc");
        filterMap.put("/test/hello2","authc");


        //设置需要某权限才能访问
        filterMap.put("/test/hello2","perms[user:root]");


        //filterMap.put("/test/*","authc");
        bean.setFilterChainDefinitionMap(filterMap);

        //设置登录失败的跳转位置
        bean.setLoginUrl("/test/testlogin");

        //设置没有权限跳转位置
        bean.setUnauthorizedUrl("/test/power");*/
    }
    //DafaultWebSecurityManager
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联userRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建realm对象  需要自定义类:1
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }
}
